<?php
declare(strict_types=1);


namespace App\Fastapp\Mapper;

use App\Fastapp\Model\FastappVersion;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * app版本列表Mapper类
 */
class FastappVersionMapper extends AbstractMapper
{
    /**
     * @var FastappVersion
     */
    public $model;

    public function assignModel()
    {
        $this->model = FastappVersion::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        // 版本号
        if (isset($params['version']) && filled($params['version'])) {
            $query->where('version', '=', $params['version']);
        }

        // 更新日期
        if (isset($params['date']) && filled($params['date']) && is_array($params['date']) && count($params['date']) == 2) {
            $query->whereBetween(
                'date',
                [ $params['date'][0], $params['date'][1] ]
            );
        }

        // app_name
        if (isset($params['app_name']) && filled($params['app_name'])) {
            $query->where('app_name', '=', $params['app_name']);
        }

        return $query->orderByDesc('id');
    }
}